package com.spz.demo.singleboot.controller.system;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.spz.demo.singleboot.bean.system.SysRoleBean;
import com.spz.demo.singleboot.bean.system.SysRoleFunctionBean;
import com.spz.demo.singleboot.bean.system.SysRoleMenuBean;
import com.spz.demo.singleboot.core.baen.PageBean;
import com.spz.demo.singleboot.core.baen.RestBean;
import com.spz.demo.singleboot.core.constant.DataValid;
import com.spz.demo.singleboot.core.constant.RestCode;
import com.spz.demo.singleboot.service.system.SysRoleFunctionService;
import com.spz.demo.singleboot.service.system.SysRoleMenuService;
import com.spz.demo.singleboot.service.system.SysRoleService;

import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 角色接口 控制器
 * @author spzmmd
 * @date 2019/05/15
 */
@Api(tags = "系统 - 角色管理", produces = "application/json")
@ApiResponses({
        @ApiResponse(code = 2000, message = "操作成功", response = RestBean.class),
        @ApiResponse(code = 5000, message = "业务操作失败：xxxx", response = RestBean.class),
        @ApiResponse(code = 5001, message = "操作失败，未知错误", response = RestBean.class),
        @ApiResponse(code = 5002, message = "参数不合法", response = RestBean.class),
        @ApiResponse(code = 4001, message = "没有权限", response = RestBean.class),
        @ApiResponse(code = 4002, message = "账号或密码错误", response = RestBean.class),
        @ApiResponse(code = 4003, message = "token错误", response = RestBean.class),
        @ApiResponse(code = 4004, message = "token过期", response = RestBean.class)
})
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    private final SysRoleService sysRoleService;
    private final SysRoleMenuService roleMenuService;
    private final SysRoleFunctionService roleFunctionService;

    public SysRoleController(SysRoleService sysRoleService, SysRoleMenuService roleMenuService, SysRoleFunctionService roleFunctionService) {
        this.sysRoleService = sysRoleService;
        this.roleMenuService = roleMenuService;
        this.roleFunctionService = roleFunctionService;
    }

    /**
     * 下拉框列表
     * @return
     */
    @RequestMapping("/dropdownList")
    public RestBean dropdownList(){
        return RestBean.ok().data(sysRoleService.findList(null));
    }

    /**
     * 分页查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "角色分页查询", response = RestBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称", dataType = "String", required = false, paramType = "query")
    })
    @RequestMapping("/listPage")
    public RestBean listPage(@ApiIgnore SysRoleBean bean, PageBean pageBean){
        return RestBean.ok().data(
                sysRoleService.findPage(bean,pageBean)
        );
    }

    /**
     * 角色详情
     * @param code
     * @return
     */
    @RequestMapping("/detail")
    public RestBean detail(String code) {
        if(ObjectUtils.isEmpty(code)){
            return RestBean.error(RestCode.DEFAULT_PARAMS_ERROR);
        }
        return RestBean.ok().data(
                sysRoleService.findByCode(code)
        );
    }

    @ApiOperation(value = "新增角色", response = RestBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称", dataType = "String", required = true, paramType = "form"),
            @ApiImplicitParam(name = "notes", value = "角色备注", dataType = "String", required = false, paramType = "form")
    })
    @PostMapping("/add")
    public RestBean add(@ApiIgnore SysRoleBean bean){
        if(bean == null || ObjectUtils.isEmpty(bean.getName())){
            return RestBean.error(RestCode.DEFAULT_PARAMS_ERROR);
        }

        // 是否重名
        int iCount = sysRoleService.findCount(SysRoleBean.builder()
                .name(bean.getName())
                .valid(DataValid.VALID)
                .build());
        if(iCount > 0){
            return RestBean.error(RestCode.DEFAULT_PARAMS_ERROR, "名称重复");
        }

        return RestBean.ok().data(
                sysRoleService.add(bean)
        );
    }

    @ApiOperation(value = "修改角色", response = RestBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "唯一标识", dataType = "String", required = true, paramType = "form"),
            @ApiImplicitParam(name = "name", value = "角色名称", dataType = "String", required = true, paramType = "form"),
            @ApiImplicitParam(name = "notes", value = "角色备注", dataType = "String", required = false, paramType = "form")
    })
    @PostMapping("/update")
    public RestBean update(@ApiIgnore SysRoleBean bean){
        if(bean == null || ObjectUtils.isEmpty(bean.getCode())) {
            return RestBean.error(RestCode.DEFAULT_PARAMS_ERROR);
        }
        return RestBean.ok().data(
                sysRoleService.updateByCode(bean)
        );
    }

    /**
     * 根据唯一标识删除一条记录
     * @param code
     * @return
     */
    @ApiOperation(value = "删除角色", response = RestBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "唯一标识", dataType = "String", required = true, paramType = "form")
    })
    @PostMapping("/delete")
    public RestBean delete(@ApiIgnore String code) {
        // 不传入id不允许删除
        if(ObjectUtils.isEmpty(code)){
            return RestBean.error(RestCode.DEFAULT_PARAMS_ERROR);
        }

        // 删除角色
        RestBean delMenuRest = sysRoleService.deleteByCode(code);

        if(delMenuRest.getCode() == RestCode.DEFAULT_SUCCESS.getCode()){
            // 删除角色菜单对应记录
            roleMenuService.delete(SysRoleMenuBean.builder()
                    .roleCode(code)
                    .build());
            // 删除角色功能对应记录
            roleFunctionService.delete(SysRoleFunctionBean.builder()
                    .roleCode(code)
                    .build());
        }

        return delMenuRest;
    }

    /**
     * 批量删除
     * @param codeList
     * @return
     */
    @ApiOperation(value = "批量删除角色", response = RestBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "codeList", value = "唯一标识", example = "88,88,88", required = true, paramType = "form")
    })
    @PostMapping("/batchDelete")
    public RestBean batchDelete(@RequestParam("codeList") List<String> codeList){
        if(ObjectUtils.isEmpty(codeList)){
            return RestBean.error(RestCode.DEFAULT_PARAMS_ERROR);
        }
        sysRoleService.deleteByCode(codeList);
        return RestBean.ok();
    }
}
